package com.chagee.store.operation.server.service.third;

import com.alibaba.fastjson.JSONObject;
import com.chagee.payment.sdk.client.PaymentClient;
import com.chagee.payment.sdk.dto.request.ConfirmOfflineRequest;
import com.chagee.payment.sdk.dto.request.CreateTradeOrderReqDto;
import com.chagee.payment.sdk.dto.request.OfflineConfirmPaymentRequest;
import com.chagee.payment.sdk.utils.AjaxResult;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @Author : hk
 * @Date : 2024/10/29
 */

@Slf4j
@Service
@RequiredArgsConstructor
public class PaymentService {

	/**
	 * 预支付接口
	 */
	public AjaxResult paymentCreateOrder(CreateTradeOrderReqDto dto, String publicKey, String privateKey, String url, String appId) {
		try {
			log.info("单号{}调用支付预下单接口,request: {}", dto.getOutOrderNo(), JSONObject.toJSONString(dto));
			AjaxResult ajaxResult = PaymentClient.createOrder(dto, publicKey, privateKey, url, appId);
			log.info("单号{}调用支付预下单接口,response{}", dto.getOutOrderNo(), JSONObject.toJSONString(ajaxResult));
			return ajaxResult;
		} catch (Exception e) {
			log.error("单号{}调用支付预下单异常,request:{},error:", dto.getOutOrderNo(), JSONObject.toJSONString(dto), e);
			return null;
		}
	}

	/**
	 * 线下支付
	 * @return
	 */
	public AjaxResult offlinePay(OfflineConfirmPaymentRequest request, String publicKey, String privateKey, String url, String appId) {

		log.info("调用钱包线下支付接口单号{},request: {}, url:{}", JSONObject.toJSONString(request.getOrderNos()), JSONObject.toJSONString(request), url);

		AjaxResult result = null;
		try {
			result = PaymentClient.confirmPayment(request, publicKey, privateKey, url, appId);
			log.info("调用钱包线下支付接口单号{},response: {}", JSONObject.toJSONString(request.getOrderNos()), JSONObject.toJSONString(result));
		} catch (Exception e) {
			log.error("调用线下支付接口异常,request:{},error:", JSONObject.toJSONString(request), e);
		}
		return result;
	}

	/**
	 * 线下支付确认
	 * @return
	 */
	public AjaxResult offlinePayConfirm(ConfirmOfflineRequest request, String publicKey, String privateKey, String url, String appId) {

		log.info("调用线下支付确认接口单号{},request: {}, url:{}", JSONObject.toJSONString(request.getOrderNoLists()), JSONObject.toJSONString(request), url);

		AjaxResult result = null;
		try {
			result = PaymentClient.offlinePayConfirm(request, publicKey, privateKey, url, appId);
			log.info("调用线下支付确认接口单号{},response: {}", JSONObject.toJSONString(request.getOrderNoLists()), JSONObject.toJSONString(result));
		} catch (Exception e) {
			log.error("调用线下支付确认接口异常,request:{},error:", JSONObject.toJSONString(request), e);
		}
		return result;
	}
}
